﻿//********************************************************************************//
//创建日期：<创建日期，2016-04-24>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：采灌井征询单
//********************************************************************************//
layui.use(['form', 'jquery', 'laydate'], function () {
    var form = layui.form()
    , layer = layui.layer
    , $ = layui.jquery
    , laydate = layui.laydate;
    /*initial load start*/
    initcontrol();
    /*initial load end*/

    /*initial*/
    function initcontrol() {
        //设置默认打印日期与回执日期
        var date = new Date();
        $("#cYM").val(date.getFullYear()+1);
        $("#printdate").val(date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate());
        date.setMonth(date.getMonth() + 1);
        $("#returndate").val(date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate());
    }
    /*end initial*/

    /*event*/
    $("#oSearch").click(function () {
        if ($("#cYM").val() == "")
        {
            layer.alert("请输入年份");
            return;
        }
        if ($("#returndate").val() == "") {
            layer.alert("请选择最后返回日期");
            return;
        }
        if ($("#printdate").val() == "") {
            layer.alert("请选择打印日期");
            return;
        }
        var ym = $("#cYM").val();
        var printdate = $("#printdate").val();
        var returndate = $("#returndate").val();
        load(ym, printdate, returndate);
    });

    //结果列表相关事件
    function bindListOperate() {
        //行点击切换选中状态
        $("#ContentList1 .datarow").each(function () {
            var cb = $(this).find("[name='liCheck']");
            $(this).find("td:gt(0)").unbind("click").click(function () {
                if ($(cb).prop("checked")) {
                    $(cb).removeProp("checked");
                }
                else {
                    $(cb).prop("checked", "checked");
                }
                countSelected();
                form.render("checkbox");
            });
        });
        
    }
    //临听全选/取消
    form.on("checkbox(selectAll)", function (data) {
        if (data.elem.checked) {
            $("#ContentList1 .datarow:visible").find("[name='liCheck']").prop("checked", "checked");
        }
        else {
            $("#ContentList1 .datarow:visible").find("[name='liCheck']").removeProp("checked");
        }
        form.render("checkbox");
        countSelected();
    });
    //临听选择
    form.on("checkbox(liCheck)", function (data) {
        form.render("checkbox");
        countSelected();
    });

    //打印
    $("#oPrint").click(function () {
        var checks = $("#ContentList1 .datarow").find("[name='liCheck']:checked");
        if(checks.length==0)
        {
            layer.alert("请选择要打印的记录");
            return;
        }
        //取得报表参数 
        var bhsArr = new Array();
        $(checks).each(function () {
            bhsArr.push($(this).attr("vid"));
        });
        var bbid = 100203;
        bbtitle = "采灌井征询单";
        var ym = $("#cYM").val();
        var printdate = $("#printdate").val();
        var returndate = $("#returndate").val();
        var params = new Array();
        params[0] = "V_Y:" + ym;
        params[1] = "V_RETURNDATE:" + returndate;
        params[2] = "V_PRINTDATE:" + printdate;
        params[3] = "V_YONGHUHS:" + bhsArr.join(",");
        wap.common.printjx('ZhangDanDY', [
           { key: 'QUERYPARAM', value: params.join("|") },
           { key: 'BAOBIAOID', value: bbid },
           { key: 'USERINFO', value: "csms" }
        ]);
        //var url = "/Pages/YongShui/15.BaoBiaoGL/BaoBiaoView.aspx?baobiaoid=" + bbid + "&baobiaocs=" + params.join("|");
        //layer.open({
        //    type: 2,
        //    title: bbtitle,
        //    shadeClose: false,
        //    shade: 0.8,
        //    area: ['1100px', '660px'],
        //    content: url
        //});
    });

    //导出
    $("#oExport").click(function () {
        var checks = $("#ContentList1 .datarow").find("[name='liCheck']:checked");
        if (checks.length == 0) {
            layer.alert("请选择要导出的记录");
            return;
        }
        //取得参数 
        var bhsArr = new Array();
        $(checks).each(function () {
            bhsArr.push($(this).attr("vid"));
        });
        var ym = $("#cYM").val();
        var printdate = $("#printdate").val();
        var returndate = $("#returndate").val();
        var url = "/ashx/QSQT/QiTa.ashx?op=CaiGuanJZXD_Export&ym=" + ym + "&printdate=" + printdate + "&returndate=" + returndate + "&yhhs=" + bhsArr.join(",");
        $("iframe").remove();
        var iframe="<iframe style='display:none' src='"+url+"'></iframe>"
        $("body").append(iframe);
        
    });
    
    /*event end*/

    /*list*/
    function createItemStr(obj) {
        var str = $("#ItemStr1 tr").prop("outerHTML");
        var itemstr, yhh, yhm, ym, printdate, returndate, youbian, dizhi;
        yhh = obj["S_YONGHUH"];
        yhm = obj["S_YONGHUM"];
        ym = obj["I_YM"];
        printdate = obj["S_PRINTDATE"];
        returndate = obj["S_RETURNDATE"];
        youbian = obj["S_YOUBIAN"];
        dizhi = obj["S_DIZHI"];
        itemstr = str.replace(new RegExp("objYongHuH", "gm"), yhh)
                        .replace(new RegExp("objYongHuM", "gm"), yhm)
                        .replace(new RegExp("objYM", "gm"), ym)
                        .replace(new RegExp("objReturnDate", "gm"), returndate)
                        .replace(new RegExp("objPrintDate", "gm"), printdate)
                        .replace(new RegExp("objYouBian", "gm"), youbian)
                        .replace(new RegExp("objDiZhi", "gm"), dizhi)
        ;
        return itemstr; 
    }

    function load(ym,printdate,returndate) {
        var index = layer.load(1);
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/QSQT/QiTa.ashx",
            data: {
                "op": "CaiGuanJZXD",
                "ym": ym,
                "printdate": printdate,
                "returndate": returndate
            },
            dataType: "json",
            success: function (rs) {
                var strs = "";
                for (var i in rs) {
                    strs = strs + createItemStr(rs[i]);
                }
                $("#lsCount").text(rs.length);
                $("#ContentList1").find(".datarow").remove();
                $("#ContentList1").append(strs);
                form.render("checkbox");
                bindListOperate();
                countSelected();
            },
            error: function (error) {
                layer.msg(error.responseText);
            }
        });
        layer.close(index);
    }

    /*end list*/

    /*logic*/
    
    //计算选中条数
    function countSelected()
    {

        $("#lsSelectedCount").text($("#ContentList1 .datarow:visible").find("[name='liCheck']:checked").length);
    }
    function verify() {
        return true;
    }

    /*logic end*/
});
